<script setup>
import {momentContentConfig} from '@/views/main/moment/config/content.config'
import {ref} from 'vue'
import useMainStore from '@/store/main'
import {momentChangeConfig} from '@/views/main/moment/config/change.config'
import useMainContent from '@/hooks/main'
import {defaultCommodityFormData} from '@/views/main/moment/config/default'

//农品数据
const commodityArr = ref([])
const mainStore = useMainStore()
mainStore.fetchListAction('commodity').then((res) => commodityArr.value = res)

const [popupRef, handleEdit] = useMainContent()

//点击通过
const handleAdopt = (moment_id) => {
  handleEdit({...defaultCommodityFormData, moment_id})
}

//选择农品
const handleChoose = (value) => {
  popupRef.value.setForm('_id', value)
  popupRef.value.setForm('count', commodityArr.value.find(item => item._id === value).count)
}

//通过事件
const handleConfirm = async (table, commodity_id, data) => {
  //获取moment_id
  const momentId = data.moment_id

  //删除data中的moment_id
  delete data.moment_id

  //修改库存信息
  await mainStore.fetchEditAction(table, commodity_id, data, false)
  //调整发布状态
  await mainStore.fetchEditAction('moment', momentId, {status: 2})
}
</script>

<template>
  <!-- 表格 -->
  <page-content :content-config="momentContentConfig">
    <template #agree="{_id, status}">
      <el-button v-if="status === 1" text bg type="success" @click="handleAdopt(_id)">
        通过
      </el-button>
    </template>
  </page-content>

  <!-- 弹出层 -->
  <page-change ref="popupRef" :change-config="momentChangeConfig" :edit-fn="handleConfirm">
    <template #commodity="{prop,value}">
      <el-select :model-value="value" @update:modelValue="handleChoose" placeholder="请选择农品">
        <el-option v-for="{_id, name} in commodityArr" :key="_id" :label="name" :value="_id"/>
      </el-select>
    </template>
  </page-change>
</template>

<style lang="scss" scoped>
</style>
